#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <pthread.h>

#include "idc_ubus_bridge.h"
#include "bridge_service.h"


bool isEmpty(const char *str) {
        if (str != NULL) {
                if (strlen(str) == 0) {
                        return true;
                }
                return false;
        }
        return true;
}

int ubus_get_bridge_info(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg)
{
        struct blob_attr *tb[__BRIDGE_INFO_MAX];
        BRIDGE_info bridge_info;
        blobmsg_parse(get_bridge_info_policy, __BRIDGE_INFO_MAX, tb, blob_data(msg), blob_len(msg));
        blob_buf_init(&b, 0);
        if(tb[BRIDGE_INFO])
        {
                char *ip = blobmsg_get_string(tb[BRIDGE_INFO]);
                //todo
                //get_bridge_info(ip, &bridge_info);


                BRIDGE_info *bridge_info = bridge_snmp_init("public", "1", ip); 

                if (bridge_info != NULL){
                        void *tb;
                        //tb = blobmsg_open_table(&b, "info");
                        printf("deviceMode:%s\n", bridge_info->deviceMode);
                        if(!isEmpty(bridge_info->deviceMode)) blobmsg_add_string(&b, "deviceMode", bridge_info->deviceMode);
                        printf("wirelessMode:%s\n", bridge_info->wirelessMode);
                        if(!isEmpty(bridge_info->wirelessMode)) blobmsg_add_string(&b, "wirelessMode", bridge_info->wirelessMode);
                        printf("channelWidth:%d\n", bridge_info->channelWidth);
                        blobmsg_add_u32(&b, "channelWidth", bridge_info->channelWidth);
                        printf("antennaGain:%d\n", bridge_info->antennaGain);
                        blobmsg_add_u32(&b, "antennaGain", bridge_info->antennaGain);
                        printf("pointDistance:%d\n", bridge_info->pointDistance);
                        blobmsg_add_u32(&b, "pointDistance", bridge_info->pointDistance);
                        printf("accessPointsNumber:%d\n", bridge_info->accessPointsNumber);
                        blobmsg_add_u32(&b, "accessPointsNumber", bridge_info->accessPointsNumber);
                        printf("encryptionMethod:%s\n", bridge_info->encryptionMethod);
                        if(!isEmpty(bridge_info->encryptionMethod)) blobmsg_add_string(&b, "encryptionMethod", bridge_info->encryptionMethod);
                        printf("portMode:%s\n", bridge_info->portMode);
                        blobmsg_add_string(&b, "portMode", bridge_info->portMode);
                        printf("mtu:%s\n", bridge_info->mtu);
                        blobmsg_add_string(&b, "mtu", bridge_info->mtu);
                        printf("ssid:%s\n", bridge_info->ssid);
                        blobmsg_add_string(&b, "ssid", bridge_info->ssid);
                        // printf("model:%s\n", bridge_info->model);
                        // blobmsg_add_string(&b, "model", bridge_info->model);
                        printf("ip:%s\n", ip);
                        blobmsg_add_string(&b, "ip", ip);
                        printf("mac:%s\n", bridge_info->mac);
                        blobmsg_add_string(&b, "mac", bridge_info->mac);
                        #if 0
                        printf("netmask:%s\n", bridge_info->netmask);
                        blobmsg_add_string(&b, "netmask", bridge_info->netmask);
                        printf("gatewayIp:%s\n", bridge_info->gatewayIp);
                        blobmsg_add_string(&b, "gatewayIp", bridge_info->gatewayIp);
                        printf("preferredDnsIP:%s\n", bridge_info->preferredDnsIP);
                        blobmsg_add_string(&b, "preferredDnsIP", bridge_info->preferredDnsIP);
                        printf("alternateDnsIP:%s\n", bridge_info->alternateDnsIP);
                        blobmsg_add_string(&b, "alternateDnsIP", bridge_info->alternateDnsIP);
                        printf("softwareVersion:%s\n", bridge_info->softwareVersion);
                        blobmsg_add_string(&b, "softwareVersion", bridge_info->softwareVersion);
                        #endif
                        //blobmsg_close_table(&b, tb);
                        //tb = blobmsg_open_table(&b, "statusData");
                        printf("workingFrequency:%d\n", bridge_info->workingFrequency);
                        blobmsg_add_u32(&b, "workingFrequency", bridge_info->workingFrequency);
                        printf("transmitPower:%d\n", bridge_info->transmitPower);
                        blobmsg_add_u32(&b, "transmitPower", bridge_info->transmitPower);
                        printf("signalStrength:%d\n", bridge_info->signalStrength);
                        blobmsg_add_u32(&b, "signalStrength", bridge_info->signalStrength);
                        printf("backgroundNoise:%d\n", bridge_info->backgroundNoise);
                        blobmsg_add_u32(&b, "backgroundNoise", bridge_info->backgroundNoise);
                        printf("polarizationSignalDifference:%d\n", bridge_info->polarizationSignalDifference);
                        blobmsg_add_u32(&b, "polarizationSignalDifference", bridge_info->polarizationSignalDifference);
                        #if 0
                        printf("transmissionCapacity:%d\n", bridge_info->transmissionCapacity);
                        blobmsg_add_u32(&b, "transmissionCapacity", bridge_info->transmissionCapacity);
                        printf("receptionThroughput:%d\n", bridge_info->receptionThroughput);
                        blobmsg_add_u32(&b, "receptionThroughput", bridge_info->receptionThroughput);
                        printf("delay:%d\n", bridge_info->delay);
                        blobmsg_add_u32(&b, "delay", bridge_info->delay);
                        printf("cinrCarrier:%d\n", bridge_info->cinrCarrier);
                        blobmsg_add_u32(&b, "cinrCarrier", bridge_info->cinrCarrier);
                        #endif
                        printf("cpu:%d\n", bridge_info->cpu);
                        blobmsg_add_u32(&b, "cpu", bridge_info->cpu);
                        #if 0
                        printf("sendBytes:%d\n", bridge_info->sendBytes);
                        blobmsg_add_u64(&b, "sendBytes", bridge_info->sendBytes);
                        printf("receivedBytes:%d\n", bridge_info->receivedBytes);
                        blobmsg_add_u64(&b, "receivedBytes", bridge_info->receivedBytes);
                        printf("runningTime:%ld\n", bridge_info->runningTime);
                        blobmsg_add_u32(&b, "runningTime", bridge_info->runningTime);
                        #endif
                        printf("deviceCurrentTime:%s\n", bridge_info->deviceCurrentTime);
                        blobmsg_add_string(&b, "deviceCurrentTime", bridge_info->deviceCurrentTime);
                        //blobmsg_close_table(&b, tb);
                        //已连接设备信息
                        tb = blobmsg_open_array(&b, "connect");
                        printf("ubus debug\n");
                        BRIDGE_info *connect = (BRIDGE_info *)bridge_info->connect;
                        for(int i = 0; i < bridge_info->connectNum; i++){
                                void *node = blobmsg_open_table(&b, NULL);
                                if(!isEmpty((connect+i)->mac)) blobmsg_add_string(&b, "mac", (connect+i)->mac);
                                if(!isEmpty((connect+i)->ip)) blobmsg_add_string(&b, "ip", (connect+i)->ip);
                                if(!isEmpty((connect+i)->deviceName)) blobmsg_add_string(&b, "deviceName", (connect+i)->deviceName);
                                blobmsg_add_u32(&b, "cpu", (connect+i)->cpu);
                                blobmsg_add_u32(&b, "transmissionCapacity", (connect+i)->transmissionCapacity);
                                blobmsg_add_u32(&b, "receptionThroughput", (connect+i)->receptionThroughput);
                                blobmsg_add_u32(&b, "signalStrength", (connect+i)->signalStrength);
                                blobmsg_add_u32(&b, "backgroundNoise", (connect+i)->backgroundNoise);
                                blobmsg_add_u32(&b, "connectTime", (connect+i)->connectTime);
                                blobmsg_add_u64(&b, "sendBytes", (connect+i)->sendBytes);
                                blobmsg_add_u64(&b, "receivedBytes", (connect+i)->receivedBytes);
                                blobmsg_close_table(&b, node);
                        }
                        blobmsg_close_table(&b, tb);
                        
                }
                ubus_send_reply(ctx, req, b.head);
                free_BridgeInfo(bridge_info);
        }
        return 0;
}  
